
export default {
  /**
 * 设置cookie信息
 * @param cname 键的名称
 * @param cvalue 键对应的值
 * @param exdays 保存时间(天数) (保存时间默认1天，不可以为0,'', null或undefine，会导致百度浏览器cookie没有存储token)
 */
  setCookie (cname, cvalue, exdays = 1) {
    const d = new Date()
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
    const expires = 'expires=' + d.toUTCString()
    document.cookie = cname + '=' + cvalue + '; path=/;' + expires
  },

  /**
 * 获取cookie信息
 * @param cname cookie存的键值
 * @returns {*}
 */
  getCookie (cname) {
    const ca = document.cookie.split(';')
    // const cokies = []
    let obj = []
    for (let i = 0; i < ca.length; i++) {
      obj = ca[i].split('=')
      if (obj[0].replace(/(^\s*)|(\s*$)/g, '') === cname && obj[1]) {
        return obj[1]
      }
    }
    return ''
  },

  /**
 * 删除cookie
 * @param cname cookie存的键值
 */
  delCookie (cname) {
    this.setCookie(cname, '', -1)
  },

  /**
 * 当前浏览器检测:FF Chrome IE11 IE10 IE9  Edge
 * @returns {*}
 */
  getCurBrowser: function () {
    const webInfo = navigator.userAgent
    const chromeVendor = navigator.vendor
    if (webInfo.indexOf('OPR') !== -1) {
      return 'Opera'
    } else if (webInfo.indexOf('Edge') !== -1) {
      return 'Edge'
    } else if (webInfo.indexOf('Firefox') !== -1) {
      return 'FF'
    } else if (webInfo.indexOf('MSIE 9.0') !== -1) {
      return 'IE9'
    } else if (webInfo.indexOf('MSIE 10.0') !== -1) {
      return 'IE10'
    } else if (webInfo.indexOf('Trident') !== -1 && webInfo.indexOf('MSIE') === -1) {
      return 'IE11'
    } else if ((chromeVendor !== '' && chromeVendor !== undefined && chromeVendor != null && chromeVendor.indexOf('Google') !== -1) || webInfo.indexOf('Chrome') !== -1) {
      return 'Chrome'
    } else if (webInfo.indexOf('Safari') !== -1) {
      return 'Safari'
    } else {
      return 'other'
    }
  },

  /**
 * 获取文件类型
 * @param fileName
 * @returns {*}
 */
  getFileType (fileName) {
    const extName = fileName.substr(fileName.lastIndexOf('.') + 1)
    switch (extName) {
      case 'txt':
        return 'txt'
      case 'rar':
        return 'rar'
      case 'zip':
        return 'zip'
      case 'doc':
      case 'docx':
        return 'doc'
      case 'ppt':
      case 'pptx':
        return 'ppt'
      case 'xls':
      case 'xlsx':
        return 'xls'
      case 'pdf':
        return 'pdf'
      case 'gif':
      case 'img':
      case 'jpeg':
      case 'jpg':
      case 'png':
      case 'svg':
        return 'img'
      default:
        return 'unKnown'
    }
  },

  /**
 * 获取文件类型对应的iconfont类名
 * @param fileName
 * @returns {*}
 */
  getFileTypeIcon (fileName) {
    const fileType = this.getFileType(fileName)
    const fileTypeIcon = {
      txt: 'icon-txt',
      rar: 'icon-rar',
      zip: 'icon-zip',
      doc: 'icon-word',
      ppt: 'icon-ppt',
      xls: 'icon-excel',
      pdf: 'icon-pdf',
      img: 'icon-pic',
      unKnown: 'icon-other-file'
    }
    return fileTypeIcon[fileType]
  },
  /**
 * 数字转中文(仅支持整数)
 * @param num String/Number
 */
  numberToChinese (num) {
    // 定义转换中文
    const chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
    // const chnUnitSection = ['', '万', '亿', '万亿', '亿亿']
    const chnUnitChar = ['', '十', '百', '千']

    let section = parseInt(num) || 0

    let strIns = ''
    let chnStr = ''
    let unitPos = 0
    let zero = true
    while (section > 0) {
      const v = section % 10
      if (v === 0) {
        if (!zero) {
          zero = true
          chnStr = chnNumChar[v] + chnStr
        }
      } else {
        zero = false
        strIns = chnNumChar[v]
        strIns += chnUnitChar[unitPos]
        chnStr = strIns + chnStr
      }
      unitPos++
      section = Math.floor(section / 10)
    }
    return chnStr
  },
  /**
 * 兼容360兼容模式的onresize事件绑定
 * @param {function} callback 业务代码函数回调
 * @param {object} option 一些选项，option.inherit：是否需要继承已有的onresize
 */
  compatible360Resize (callback, option = {}) {
    // 缓存之前存在的 window.onresize
    const prevFunc = window.onresize

    // 避免360兼容模式resize失效
    let resize360Timer = null
    let resizeFucCache = null
    let resizeReady = false

    window.onresize = (ev) => {
      // 如果需要继承以前的resize则继承调用
      if (option.inherit && prevFunc) prevFunc(ev)

      // 业务代码...
      callback()

      // 兼容360安全模式兼容模式 onresize
      if (!resizeReady) {
        if (resize360Timer) {
          clearTimeout(resize360Timer)
        }
        resize360Timer = setTimeout(() => {
          window.onresize = resizeFucCache
          resizeReady = true
        }, 250)
      }
    }

    // 避免360兼容模式resize失效
    resizeFucCache = window.onresize
  },

  // 获取浏览器参数
  getUrlParams () {
    const url = window.location.href // 获取url中"?"符后的字串
    // console.log("url:"+url);
    const theRequest = {}
    const idx = url.indexOf('?')
    if (idx !== -1) {
      const str = url.substr(idx + 1)
      const strs = str.split('&')
      for (let i = 0; i < strs.length; i++) {
        const objK = strs[i].split('=')[0]
        const objV = decodeURIComponent(strs[i].split('=')[1]) // unescape(strs[i].split("=")[1]);
        // console.log(">>>>>>created=name:"+objK+"=value="+objV);
        theRequest[objK] = objV
      }
    }
    return theRequest
  }

}

/**
 * 
 * @param {*} url 下载的url
 */
export function downloadUrl (url) {
  const u = navigator.userAgent
  // const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端

  if (isiOS) { // ios终端
    window.location.href = encodeURI(url)
  } else {
    window.open(encodeURI(url))
  }
  // if(!url)
  // {
  //   return
  // }
  // if(!name)
  // {
  //   name=url.substring(url.lastIndexOf('/') + 1);
  // }
  // const downloadFilename = name;
  // // 创建隐藏的可下载链接
  // const elementLink = document.createElement('a');
  // elementLink.setAttribute('download', downloadFilename);
  // elementLink.setAttribute('target', '_blank');
  // elementLink.style.display = 'none';
  // elementLink.href = url;
  // // 触发点击
  // document.body.appendChild(elementLink);
  // elementLink.click();
  // // 然后移除
  // document.body.removeChild(elementLink);
}
